package swd.work.generator.bean.dotnet;

import java.io.BufferedWriter;
import java.util.Hashtable;

import com.pub.database.QueryBean;
import com.pub.tools.PublicFunction;

import swd.work.generator.base.dotnet.BeanExtend;
import swd.work.generator.base.dotnet.BeanImplement;

public class VOBean extends BeanExtend implements BeanImplement {

	public VOBean(String pstrGeneratorTemplate,String pstrFramework, QueryBean pqbnDAO, String pstrUserNO)
			throws Exception {
		super(pstrGeneratorTemplate,pstrFramework, pqbnDAO, pstrUserNO);

	}

	/**
    *
    * @param pbwrPrgNO BufferedWriter 輸出物件
    * @param pstrSysNO String 系統編號
    * @param pstrBeanName String 要產生的JavaBean 名稱
    * @param pstbTabStr StingBuffer 縮排字集
    * @param pstrTabChar String 縮排字元
    * @return String
    */
	@Override
	public String outputBeanHeader(BufferedWriter pbwrPrgNO,
			String pstrSysNO, String pstrBeanName,
			StringBuffer pstbTabStr, String pstrTabChar) throws Exception{
		
		super.outputJavaCode(pbwrPrgNO,gstbTabStr,gstrTabChar,super.gstrVOBeanUsing,1);
		
		super.outputJavaCode(pbwrPrgNO,gstbTabStr,gstrTabChar,"namespace " + super.gstrSysNO.toUpperCase() + ".DAL.VO",2);
		super.outputJavaCode(pbwrPrgNO,gstbTabStr,gstrTabChar,"{",1);
		super.outputJavaCode(pbwrPrgNO,gstbTabStr,gstrTabChar,"[Serializable]",1);
		super.outputJavaCode(pbwrPrgNO,gstbTabStr,gstrTabChar,"public sealed class " + PublicFunction.replace(pstrBeanName, ".cs", "", false) + "{",1);
		
		return null;
	}
	
	/**
	 * 
	 * @see swd.work.generator.base.dotnet.BeanImplement#createBean(java.lang.String)
	 */
	@Override
	public String createBean(String pstrTableName) throws Exception{
        String lstrCreateResult = "";
        String lstrBeanFileName = super.TransferEntityToCodeName(pstrTableName) + "VO.cs";
        BufferedWriter lbwrPrgNO = null;

        try {
            lbwrPrgNO = super.getFwrPrgNO(lstrBeanFileName, "ASP_FRAME_PATH",
                                          "\\DAL\\VO\\", gstrUserNO);

            outputBeanHeader(lbwrPrgNO, gstrSysNO,
                                   lstrBeanFileName, gstbTabStr,
                                   gstrTabChar);
            
            outputPropertyVarible(lbwrPrgNO, pstrTableName, gstbTabStr, gstrTabChar);
            
    		super.outputJavaCode(lbwrPrgNO,gstbTabStr,gstrTabChar,"}",1);
    		super.outputJavaCode(lbwrPrgNO,gstbTabStr,gstrTabChar,"}",1);
        }
        catch(Exception e){
        	e.printStackTrace();
        }
        finally {
            try {
                lbwrPrgNO.flush();
                lbwrPrgNO.close();
                lbwrPrgNO = null;
            } catch (Exception e) {
                System.out.println("cant flush Action.java File!!");
                e.printStackTrace();
            }  
        }
        return lstrCreateResult;
	}

    private void outputPropertyVarible(BufferedWriter pbwrPrgNO,
			String pstrTableName, StringBuffer pstbTabStr, String pstrTabChar) throws Exception{
		Hashtable<String,Hashtable<String,Object>> lhtWkfLayout = null;
		
		try{
			lhtWkfLayout = super.getFieldsByTable(pstrTableName);
			
			if(super.gstrFramework.equals(".net3.5")){
				outputPropertyForNet35(pbwrPrgNO,lhtWkfLayout,pstbTabStr,pstrTabChar);
			}
			
		}
		catch(Exception e){
			e.printStackTrace();
			throw new Exception(e);
		}
		
	}

	private void outputPropertyForNet35(BufferedWriter pbwrPrgNO,
			Hashtable<String, Hashtable<String, Object>> phtWkfLayout,
			StringBuffer pstbTabStr, String pstrTabChar) throws Exception{
		
		Hashtable<String,Object> lhtWkfLayoutData=null;
		StringBuffer lstbCode = null;
		
		try{
			lstbCode = new StringBuffer();
			
			for(int i=0;i<phtWkfLayout.size();i++){
				lhtWkfLayoutData = (Hashtable<String,Object>)phtWkfLayout.get(String.valueOf(i));
				
				if(!lstbCode.toString().trim().equals("")){
					lstbCode.delete(0, lstbCode.length());
				}
				
				super.outputJavaCode(pbwrPrgNO,gstbTabStr,gstrTabChar, "/// <summary>" ,2);
				super.outputJavaCode(pbwrPrgNO,gstbTabStr,gstrTabChar, "/// " + lhtWkfLayoutData.get("FLD_NAME").toString() ,1);
				super.outputJavaCode(pbwrPrgNO,gstbTabStr,gstrTabChar, "/// </summary>" ,1);
				
				lstbCode.append("public ");
				lstbCode.append(super.getFldType(lhtWkfLayoutData.get("FLD_KIND").toString(), lhtWkfLayoutData.get("WIDE").toString()));
				lstbCode.append(" ");
				lstbCode.append(super.TransferFieldToCodeName(lhtWkfLayoutData.get("FLD_NO").toString()));
				lstbCode.append(" {");
				
				super.outputJavaCode(pbwrPrgNO,gstbTabStr,gstrTabChar, lstbCode.toString() ,1);
				super.outputJavaCode(pbwrPrgNO,gstbTabStr,gstrTabChar, "set; get;" ,1);
				super.outputJavaCode(pbwrPrgNO,gstbTabStr,gstrTabChar, "}" ,1);
				
			}
		}
		catch(Exception e){
			e.printStackTrace();
			throw new Exception(e);
		}
		
	}
}
